/**
 * Created by djh on 2016/11/8.
 * 注册、登录公用js
 *
 */
define(function(require,exports){
   var common = require("common");
   var validate = require("validate");

    //获取和失去焦点状态
    $(".form-control").focus(function(){
        $(this).parent(".row").addClass("focus");
    }).blur(function(){
        $(this).parent(".row").removeClass("focus");
    });

    exports.reg = function() {
        if(!!exports.wrong){
          common.error(exports.wrong)
        }

        //点击协议选中checkbox
        $(".tip").on("click","a",function(){
           $(".tip").find("input").prop("checked",true);
        });

        //兼容placeholder
        common.ieTo_placeholder(document.getElementsByTagName('input'), true);

        var $sub_btn = $(".sub_btn");
        var $form = $(".form");
        var $getCodeBtn = $('[data-act="getSMS"]');
        var $mobile = $("#mobile");

        $sub_btn.on("click",function(){
            $form.submit();
        });
        //回车提交表单
        common.inputEnterCallback($form.find("input"),function(){
            $form.submit();
        });
        $form.validate({
            ignore: ".ignore",
            rules:{
                username:{required: true,isMobile:true,
                    remote:{
                        url: '/member/reg/checkMobile',
                        cache: false,
                        async: false,
                        data: {
                            'username': function(){
                                return $mobile.val();
                            }
                        }
                    }
                },
                pwd:{required: true,pwd:[true]},
                vcode:{required: true},
                agree:{required: true}
            },
            messages:{
                username:{required: "请输入用户名",remote:"该手机号码已被注册"},
                pwd:{required: "请输入密码"},
                vcode:{required: "请输入验证码"},
                agree:{required: "您还没同意开户协议"}
            },
            errorContainer:".errorWrap",
            errorLabelContainer: '.errorWrap',
            wrapper: 'li',
            success: function (label, element) {
                $(element).parent().removeClass("error");
                var elementName = element.getAttribute('name'),
                    inValid = element.getAttribute('aria-invalid');
                if(elementName == 'username' && inValid == 'false'){
                    $getCodeBtn.data("active",true).addClass('active').prop("disabled",false);
                }
            },
            highlight: function (element, errorClass) {
                $(element).parent().addClass(errorClass);
                var elementName = element.getAttribute('name'),
                    inValid = element.getAttribute('aria-invalid');
                if(elementName == 'username' && inValid == 'true'){
                    $getCodeBtn.data("active",true).removeClass('active').prop("disabled",true);
                }
            },
           submitHandler:function(f){
               layer.load();
			   f.submit();

           }

        });
        //发送短信验证码
        common.getCode($getCodeBtn,null,$mobile,"/member/reg/getSMS");
    };


});
